package cn.lcc.controller;


import cn.lcc.controller.fallback.FallBackUtilFactory;
import cn.lcc.domain.Order;
import cn.lcc.domain.User;
import cn.lcc.feignclient.IUserFeignClient;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IUserFeignClient userFeignClient;

    @GetMapping("/{id}")
    @SentinelResource(value = "orderById",blockHandler ="userFallBack",blockHandlerClass= FallBackUtilFactory.class
        ,fallback = "callback"
    )
    public Order get(@PathVariable("id") Long id){
        User user = userFeignClient.get(id);
        return new Order(id,"22222",user);
    }

    public Order callback(@PathVariable("id") Long id, BlockException e){
        e.printStackTrace();

        return new Order(-1L,"服务暂时不可用",null);
    }
    /*
    public Order userFallBack(@PathVariable("id") Long id, BlockException e){
        e.printStackTrace();

        return new Order(-1L,"您访问的数据太热门，请稍后再试",null);
    }
    */

}
